package com.richard.home.model.repository

import com.richard.base.net.bean.KsResult
import com.richard.base.net.model.repository.KsBaseRepository
import com.richard.home.model.api.HomeService
import com.richard.home.model.bean.PoetryDetail
import com.richard.home.model.bean.PoetryWord
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject


/**
 ***************************************
 * 项目名称: Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020-02-05     18:29
 * 用途:首页 Repository 实现类
 ***************************************
 */
class HomeRepositoryImpl: HomeRepository, KsBaseRepository() {


    /**
     * 获取推荐图片
     */
    override suspend fun getHomeRecPoetry(pageSize: Int): KsResult<MutableList<PoetryWord>> {
        val jsonObject = JSONObject()
        with(jsonObject) {
            put("pageSize", pageSize)
        }
        return safeApiCall(call={requestHomeRecImages(jsonObject.toString().toRequestBody())})
    }

    /**
     * 获取诗词详情
     */
    override suspend fun getPoetryDetail(poetryId: Int): KsResult<PoetryDetail> {
        return safeApiCall(call={requestPoetryDetail(poetryId.toString())})
    }


    private suspend fun requestHomeRecImages(requestBody: RequestBody):KsResult<MutableList<PoetryWord>> {
        return executeResponse(getService(HomeService::class.java).homeRecPoetry(requestBody))
    }

    private suspend fun requestPoetryDetail(poetryId: String):KsResult<PoetryDetail> {
        return executeResponse(getService(HomeService::class.java).homePoetryDetail(poetryId))
    }

}